package com.example.service.Impl;

import com.example.dao.BookDao;
import com.example.service.BorrowService;
import com.example.dao.BorrowDao;
import com.example.model.Borrow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BorrowServiceImpl implements BorrowService {
    private BorrowDao borrowDao;
    private BookDao bookDao;
    @Autowired
    public BorrowServiceImpl(BorrowDao borrowDao, BookDao bookDao) {
        this.borrowDao = borrowDao;
        this.bookDao = bookDao;
    }

    @Override
    @Transactional
    public void borrowBook(Borrow borrow) {
        if(bookDao.getBookById(borrow.getBook_id())!=null) {
            borrowDao.borrowBook(borrow);
            bookDao.deleteBook(borrow.getBook_id());
        }
        else{
            System.out.printf("图书馆中没有这本书");
        }
    }

    @Override
    @Transactional
    public void returnBook(int borrowId) {
        borrowDao.returnBook(borrowId);
    }

    @Override
    @Transactional
    public Borrow getBorrowById(int borrowId) {
        return borrowDao.getBorrowById(borrowId);
    }
}
